package com.jin.spirng_boot_learn.rbtree;

import java.util.LinkedList;
import java.util.Queue;

public class TreePrinter {

    static Queue<Node> nodeQueue = new LinkedList<>();


    public static void print(Node root) {
        if(root ==null) System.out.println("empty tree");
        nodeQueue.clear();
        Integer thisLevelCount = 0;
        nodeQueue.add(root);
        thisLevelCount++;

        while (!nodeQueue.isEmpty()) {
            Integer tempCount = 0;
            for (int i = 0; i < thisLevelCount; i++) {
                Node node = nodeQueue.poll();
                printNode(node);
                if(node.getLeft()!=null){
                    nodeQueue.add(node.getLeft());
                    tempCount++;
                }
                if(node.getRight()!=null) {
                    nodeQueue.add(node.getRight());
                    tempCount++;
                }

            }
            thisLevelCount = tempCount;
            System.out.println(); // 换行
        }
    }

    private static void printNode(Node node) {
        if (node.getColor() == Node.BLACK) {
            System.out.print(String.format("b%db ", node.getValue()));
        }else{
            System.out.print(String.format("r%dr ", node.getValue()));
        }
    }



    public static void main(String[] args) {
    }

}
